package com.qdlc.p2p.biz.service.user.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.user.UserEmailService;
import com.qdlc.p2p.biz.util.UserLoginUtil;
import com.qdlc.p2p.biz.util.ValidCodeSender;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.core.redis.JedisCacheFactory;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dao.UserIdentifyDao;
import com.qdlc.p2p.dal.dao.UserLoginExtDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.dto.UserLoginExt;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 用户邮箱
 * @author linya 2016-02-15
 */
@Service
public class UserEmailServiceImpl implements UserEmailService {

	@Resource
	UserDao userDao;
	
	@Resource
	UserLoginExtDao userLoginExtDao;
	
	@Resource
	UserIdentifyDao userIdentifyDao;
	
	/**
	 * 前置条件检测:
	 * 1. 检测验证码是否为空
	 * 2. 检测邮箱是否为空
	 * 3. 检测邮箱格式是否正确
	 * 4. 检测验证码正确性
	 * @param code
	 * @param email
	 * @return
	 */
	private ReturnResult preCheck(long userId, String code, String email){
        
		if (StringUtil.isBlank(code)) {
			return new ReturnResult("验证码为空!");
		}
        
		if (StringUtil.isBlank(email)) {
        	return new ReturnResult("邮箱为空!");
		}
        
		if (!StringUtil.isEmail(email)) {
        	return new ReturnResult("邮箱格式不正确!");
		}
        
        //检测验证码正确性
		String key = ValidCodeSender.wrapKey(userId, NoticeTplNid.NOTICE_BIND_EMAIL);
        String cacheCode = JedisCacheFactory.newInstance(String.class).get(key);
        if(StringUtils.isBlank(cacheCode)){
        	return new ReturnResult("验证码不存在或已经过期,请重新获取!");
        }
        
        if(!cacheCode.equals(code)){
        	return new ReturnResult("验证码错误!");
        }
        
		return ReturnResult.SUCCESS;
	}
	
	/**
	 * 修改用户邮箱:
	 * 1. 检测前置条件
	 * 2. 修改邮箱
	 * 3. 更改UserExt信息
	 * @param user
	 * @param code
	 * @param email
	 * @author linya 2016-02-18
	 */
	@Override
	public ReturnResult modifyEmail(User user, String code, String email){
		
		//前置条件检测
		ReturnResult result = preCheck(user.getUserId(), code, email);
		if(result.isError()){
			return result;
		}
		
		//修改用户邮箱相关联的表
		modifyRegistEmail(user, email);
		
		//更新心跳
		UserLoginUtil.heartbit(true);
		
		return ReturnResult.SUCCESS;
	}
	
	/**
	 * 注册用户邮箱，以及相关联的信息
	 * @param user
	 * @param newEmail
	 * @author linya 2016-02-18
	 */
	@Override
	public void modifyRegistEmail(User user, String newEmail){
		
		//修改UserLoginExt
		String oldEmail = user.getEmail();
		if(StringUtils.isNotBlank(oldEmail)){
			userLoginExtDao.deleteUsername(user.getUserId(), oldEmail);
		}
		UserLoginExt userExt = new UserLoginExt();
		userExt.setUserId(user.getUserId());
		userExt.setUserName(newEmail);
		userExt.setAddTime(DateUtil.getNow());
		userLoginExtDao.merge(userExt);
		
		//修改UserIdentify
		UserIdentify userIdentify = UserUtils.getUserIdentify(user.getUserId());
		userIdentify.setEmailStatus(1);
		userIdentifyDao.update(userIdentify);
		
		//修改User
		user.setEmail(newEmail);
		userDao.update(user);
		
	}
	
}
